package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.view.common.BaseActivity;

public class ScreeningActivity extends BaseActivity implements View.OnClickListener {

    UserPresenter presenter;
    int type;
    String cate = "", addr = "", cateid = "";

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.address)
    TextView address;
    @Bind(R.id.category)
    TextView category;
    @Bind(R.id.screen_address_btn)
    RelativeLayout screenAddressBtn;
    @Bind(R.id.screen_cate_btn)
    RelativeLayout screenCateBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screening);
        ButterKnife.bind(this);

        presenter = new UserPresenterImp();
        type = getIntent().getIntExtra(Constant.TO_SCREENING, 0);
        cate = getIntent().getStringExtra(Constant.TO_SCREENING_CATE);
        addr = getIntent().getStringExtra(Constant.TO_SCREENING_CITY);

        if (cate != null) {
            category.setText(cate);
        }
        if (addr != null) {
            address.setText(addr);
        }

        initToolbar();
        initClickBtn();
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.screen_address_btn: {
                intent = new Intent(this, AddressSelectActivity.class);
                intent.putExtra(Constant.TO_ADDRESS_SCREEN, type);
                startActivityForResult(intent, Constant.SEARCH_ADDRESS_TO_RESULT);
                break;
            }
            case R.id.screen_cate_btn: {
                intent = new Intent(this, CateSelectActivity.class);
                intent.putExtra(Constant.TO_CATE_SCREEN, type);
                startActivityForResult(intent, Constant.SEARCH_CATE_TO_RESULT);
                break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Constant.SEARCH_CATE_TO_RESULT) {
            cate = data.getStringExtra(Constant.SEARCH_CATE_TITLE);
            cateid = data.getStringExtra(Constant.SEARCH_CATE_ID);

            cateid = cateid.isEmpty() || cateid == null ? "" : cateid;
            cate = cate.isEmpty() || cate == null ? "" : cate;

            if (!cate.isEmpty()) {
                category.setText(cate);
            }
        }
        if (resultCode == Constant.SEARCH_ADDRESS_TO_RESULT){
            addr = data.getStringExtra(Constant.SEARCH_ADDRESS_TITLE);
            addr = addr.isEmpty() || addr == null ? "" : addr;
            if (!addr.isEmpty()) {
                address.setText(addr);
            }
        }
    }

    private void initClickBtn() {
        screenAddressBtn.setOnClickListener(this);
        screenCateBtn.setOnClickListener(this);
    }

    private void initToolbar() {
        toolbar.setTitle("筛选");
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(Constant.SEARCH_SCREEN_ADDRESS, addr);
                intent.putExtra(Constant.SEARCH_SCREEN_CATE, cateid);
                setResult(Constant.SEARCH_SCREEN, intent);
                finish();
            }
        });
    }


}
